package view;

import controller.NameChangeListener;
import controller.PlayerControlListener;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Insets;
import java.awt.event.ActionListener;
import javax.swing.*;
import util.Images;
import view.tool.ToolType;

/**
 * The MenuPanel contains the controls for the Player, the editable name of the
 * Piece and buttons to access the effects and options tools.
 */
public class MenuPanel extends JPanel {

	public static final int MENU_HEIGHT = 24;

	private JToggleButton playButton;
	private JToggleButton pauseButton;
	private JButton stopButton;

	private JTextField nameField;

	private JToggleButton effectsButton;
	private JToggleButton optionsButton;


	public MenuPanel() {
		this.setOpaque(false);
		this.setLayout(new BorderLayout());

		playButton = new JToggleButton(new ImageIcon(Images.getPlayIcon(MENU_HEIGHT)));
		playButton.setActionCommand("PLAY");
		playButton.setMargin(new Insets(0, 0, 0, 0));

		pauseButton = new JToggleButton(new ImageIcon(Images.getPauseIcon(MENU_HEIGHT)));
		pauseButton.setActionCommand("PAUSE");
		pauseButton.setMargin(new Insets(0, 0, 0, 0));

		stopButton = new JButton(new ImageIcon(Images.getStopIcon(MENU_HEIGHT)));
		stopButton.setActionCommand("STOP");
		stopButton.setMargin(new Insets(0, 0, 0, 0));

		nameField = new JTextField("Name of Piece is Quite Long");
		nameField.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		nameField.setBackground(Color.DARK_GRAY);
		nameField.setForeground(Color.WHITE);
		nameField.setHorizontalAlignment(JTextField.CENTER);

		effectsButton = new JToggleButton("Effects");
		effectsButton.setActionCommand(ToolType.EFFECTS.toString());
		effectsButton.setMargin(new Insets(0, 0, 0, 0));

		optionsButton = new JToggleButton(new ImageIcon(Images.getOptionsIcon(MENU_HEIGHT)));
		optionsButton.setActionCommand(ToolType.OPTIONS.toString());
		optionsButton.setMargin(new Insets(0, 0, 0, 0));

		playButton.setAlignmentY(TOP_ALIGNMENT);
		pauseButton.setAlignmentY(TOP_ALIGNMENT);
		stopButton.setAlignmentY(TOP_ALIGNMENT);
		effectsButton.setAlignmentY(TOP_ALIGNMENT);
		optionsButton.setAlignmentY(TOP_ALIGNMENT);

		JPanel playbackPanel = new JPanel();
		playbackPanel.setLayout(new BoxLayout(playbackPanel, BoxLayout.X_AXIS));
		playbackPanel.setOpaque(false);

		final int spacing = 4;
		playbackPanel.add(Box.createHorizontalStrut(spacing));
		playbackPanel.add(playButton);
		playbackPanel.add(Box.createHorizontalStrut(spacing));
		playbackPanel.add(pauseButton);
		playbackPanel.add(Box.createHorizontalStrut(spacing));
		playbackPanel.add(stopButton);
		playbackPanel.add(Box.createHorizontalStrut(spacing));

		JPanel rightPanel = new JPanel();
		rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.X_AXIS));
		rightPanel.setOpaque(false);

		rightPanel.add(Box.createHorizontalStrut(spacing));
		rightPanel.add(effectsButton);
		rightPanel.add(Box.createHorizontalStrut(spacing));
		rightPanel.add(optionsButton);
		rightPanel.add(Box.createHorizontalStrut(spacing));

		this.add(playbackPanel, BorderLayout.WEST);
		this.add(nameField, BorderLayout.CENTER);
		this.add(rightPanel, BorderLayout.EAST);
		this.add(Box.createVerticalStrut(2), BorderLayout.SOUTH);

		this.setState(PlaybackState.PAUSE);
	}

	public JToggleButton getPauseButton() {
		return pauseButton;
	}

	public JToggleButton getEffectsButton() {
		return effectsButton;
	}

	public JToggleButton getOptionsButton() {
		return optionsButton;
	}

	public void setPieceName(String newName) {
		nameField.setText(newName);
	}

	public String getPieceName() {
		return nameField.getText();
	}

	/**
	 * Sets the state of the playback controls.<br>
	 * If paused, the play button is lifted, if played the pause button is lifted.
	 */
	public final void setState(PlaybackState state) {
		playButton.setSelected(state == PlaybackState.PLAY);
		pauseButton.setSelected(state == PlaybackState.PAUSE);
	}

	public void setPlayerControlListener(PlayerControlListener pcl) {
		//Remove existing listeners to avoid conflicts.
		for(ActionListener al : playButton.getActionListeners()) {
			playButton.removeActionListener(al);
		}
		for(ActionListener al : pauseButton.getActionListeners()) {
			pauseButton.removeActionListener(al);
		}
		for(ActionListener al : stopButton.getActionListeners()) {
			stopButton.removeActionListener(al);
		}

		playButton.addActionListener(pcl);
		pauseButton.addActionListener(pcl);
		stopButton.addActionListener(pcl);
	}

	public void setNameChangeListener(NameChangeListener ncl) {
		for(ActionListener al : nameField.getActionListeners()) {
			nameField.removeActionListener(al);
		}
		nameField.addActionListener(ncl);
	}
}
